home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / gtk-2.0 / demo / entry_completion.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-25  |  2.6 KB  |  98 lines

  1. /* Entry Completion
  2.  *
  3.  * GtkEntryCompletion provides a mechanism for adding support for
  4.  * completion in GtkEntry.
  5.  *
  6.  */
  7.  
  8. #include <gtk/gtk.h>
  9.  
  10. static GtkWidget *window = NULL;
  11.  
  12. /* Creates a tree model containing the completions */
  13. GtkTreeModel *
  14. create_completion_model (void)
  15. {
  16.   GtkListStore *store;
  17.   GtkTreeIter iter;
  18.   
  19.   store = gtk_list_store_new (1, G_TYPE_STRING);
  20.  
  21.   /* Append one word */
  22.   gtk_list_store_append (store, &iter);
  23.   gtk_list_store_set (store, &iter, 0, "GNOME", -1);
  24.  
  25.   /* Append another word */
  26.   gtk_list_store_append (store, &iter);
  27.   gtk_list_store_set (store, &iter, 0, "total", -1);
  28.  
  29.   /* And another word */
  30.   gtk_list_store_append (store, &iter);
  31.   gtk_list_store_set (store, &iter, 0, "totally", -1);
  32.   
  33.   return GTK_TREE_MODEL (store);
  34. }
  35.  
  36.  
  37. GtkWidget *
  38. do_entry_completion (GtkWidget *do_widget)
  39. {
  40.   GtkWidget *vbox;
  41.   GtkWidget *label;
  42.   GtkWidget *entry;
  43.   GtkEntryCompletion *completion;
  44.   GtkTreeModel *completion_model;
  45.   
  46.   if (!window)
  47.   {
  48.     window = gtk_dialog_new_with_buttons ("GtkEntryCompletion",
  49.                       GTK_WINDOW (do_widget),
  50.                       0,
  51.                       GTK_STOCK_CLOSE,
  52.                       GTK_RESPONSE_NONE,
  53.                       NULL);
  54.     gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  55.  
  56.     g_signal_connect (window, "response",
  57.               G_CALLBACK (gtk_widget_destroy), NULL);
  58.     g_signal_connect (window, "destroy",
  59.               G_CALLBACK (gtk_widget_destroyed), &window);
  60.  
  61.     vbox = gtk_vbox_new (FALSE, 5);
  62.     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
  63.     gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
  64.  
  65.     label = gtk_label_new (NULL);
  66.     gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
  67.     gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  68.  
  69.     /* Create our entry */
  70.     entry = gtk_entry_new ();
  71.     gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
  72.  
  73.     /* Create the completion object */
  74.     completion = gtk_entry_completion_new ();
  75.  
  76.     /* Assign the completion to the entry */
  77.     gtk_entry_set_completion (GTK_ENTRY (entry), completion);
  78.     g_object_unref (completion);
  79.     
  80.     /* Create a tree model and use it as the completion model */
  81.     completion_model = create_completion_model ();
  82.     gtk_entry_completion_set_model (completion, completion_model);
  83.     g_object_unref (completion_model);
  84.     
  85.     /* Use model column 0 as the text column */
  86.     gtk_entry_completion_set_text_column (completion, 0);
  87.   }
  88.  
  89.   if (!GTK_WIDGET_VISIBLE (window))
  90.     gtk_widget_show_all (window);
  91.   else
  92.     gtk_widget_destroy (window);
  93.  
  94.   return window;
  95. }
  96.  
  97.  
  98.